﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace webclonegame.FormulariosPrivada
{
    public partial class Formulario_Reportes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string consulta;
            string conexion = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\clonegame.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlDataSource SqlDataSource;

            // Usuario Actual
            Persistencia_y_capa_de_negocio.ENUsuario usuario = (Persistencia_y_capa_de_negocio.ENUsuario)Session["UsuarioActual"];

            // Consulta Reportes Atacantes

            consulta = "SELECT r.fecha AS Fecha, u.cuenta AS Atacante, u2.cuenta AS Defensor, u3.cuenta As Ganador, r.ataqueA AS AtaqueAt, r.defensaA AS DefensaAt, r.ataqueD AS AtaqueDe, r.defensaD AS DefensaDe FROM reporte AS r INNER JOIN escuadron AS e ON r.atacante = e.id INNER JOIN usuarios AS u ON e.usuario = u.id INNER JOIN usuarios As u2 ON r.defensor = u2.id INNER JOIN usuarios AS u3 ON r.ganador = u3.id WHERE (u.id =" + usuario.IDUsuario.ToString() + ") ORDER BY Fecha";
            SqlDataSource = new SqlDataSource(conexion, consulta);

            gvReportesA.DataSource = SqlDataSource;
            gvReportesA.DataBind();

            // Consulta Reportes Defensores

            consulta = "SELECT r.fecha AS Fecha, u.cuenta AS Atacante, u2.cuenta AS Defensor, u3.cuenta As Ganador, r.ataqueA AS AtaqueAt, r.defensaA AS DefensaAt, r.ataqueD AS AtaqueDe, r.defensaD AS DefensaDe FROM reporte AS r INNER JOIN escuadron AS e ON r.atacante = e.id INNER JOIN usuarios AS u ON e.usuario = u.id INNER JOIN usuarios As u2 ON r.defensor = u2.id INNER JOIN usuarios AS u3 ON r.ganador = u3.id WHERE (u2.id =" + usuario.IDUsuario.ToString() + ") ORDER BY Fecha";
            SqlDataSource = new SqlDataSource(conexion, consulta);

            gvReportesD.DataSource = SqlDataSource;
            gvReportesD.DataBind();
        }

        protected void gvReportesA_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string consulta;
            string conexion = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\clonegame.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlDataSource SqlDataSource;

            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gvReportesA.Rows[index];

            consulta = "SELECT texto AS Reporte FROM reporte WHERE (fecha = '" + row.Cells[1].Text + "')";
            SqlDataSource = new SqlDataSource(conexion, consulta); 

            gvReportesA.DataSource = SqlDataSource;
            gvReportesA.DataBind();

            row = gvReportesA.Rows[index];
            consulta = row.Cells[1].Text;

            string texto = "";
            for (int i = 0; i < consulta.Length; i++)
            {
                if (consulta[i] != '/')
                {
                    texto += consulta[i];
                }
                else
                {
                    texto += "<br/>";
                }
            }

            row.Cells[1].Text = texto;
        }

        protected void gvReportesD_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string consulta;
            string conexion = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\clonegame.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlDataSource SqlDataSource;

            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gvReportesD.Rows[index];

            consulta = "SELECT texto AS Reporte FROM reporte WHERE (fecha = '" + row.Cells[1].Text + "')";
            SqlDataSource = new SqlDataSource(conexion, consulta);

            gvReportesA.DataSource = SqlDataSource;
            gvReportesA.DataBind();

            row = gvReportesA.Rows[index];
            consulta = row.Cells[1].Text;

            string texto = "";
            for (int i = 0; i < consulta.Length; i++)
            {
                if (consulta[i] != '/')
                {
                    texto += consulta[i];
                }
                else
                {
                    texto += "<br/>";
                }
            }

            row.Cells[1].Text = texto;
        }
    }
}
